<?php # HotelListingSearch.php
require "hotels.class.php";
require "hotelcharacteristics.class.php";
require "address.class.php";
require "country.class.php";
require "star_ratings.class.php";

/*
 * Specify the field names that are in the form.
 */
$allowedFields = array(
 			'stars',
			'state',
			'country',
			'search'
);

$error = false;
$message = array();
foreach($_GET AS $key => $value)
{
	// first make sure this is an allowed field
	if(!in_array($key, $allowedFields))
	{
		$error = true;
		$message['error'] = 'The field '.$key.' does not exist.';
	}
}

if (!$error) {
	if (isset($_GET['stars'])){
		$searchStars = $_GET['stars'];
	}
	else {
		$searchStars = '';
	}
	if (isset($_GET['state'])){
		$searchState = $_GET['state'];
	}
	else {
		$searchState = '';
	}
	if (isset($_GET['country'])){
		$searchCountry = $_GET['country'];
	}
	else {
		$searchCountry = '';
	}
	$hotels = Hotels::loadAll();
	$hotel_array = array();
	$return_arr = array();
	
	$count = count($hotels);
	$total = 0;
	for ($i = 0; $i < $count; $i++) {
		$hotelId = $hotels[$i]->getHotelId();
		$hotelName = $hotels[$i]->getHotelName();
		$hotelURL = $hotels[$i]->getHotelURL();
		$address = $hotels[$i]->getAddress();
		$country = $hotels[$i]->getCountry();
		$starRating = $hotels[$i]->getStarRatings();
		$search1 = $searchStars;
		$search2 = $searchState;
		$search3 = $searchCountry;
		if($searchStars == '') {
			$search1 = $starRating->getStarRatingId();
		}
		if($searchState == '') {
			$search2 = $address->getStateOrProvince();
		}
		if($searchCountry == '') {
			$search3 = $country->getCountryCode();
		}
		
		if($search1 == $starRating->getStarRatingId() && 
			$search2 == $address->getStateOrProvince() && 
			$search3 == $country->getCountryCode()) {
				$hotel_array['hotel_id'] = $hotelId;
				$hotel_array['hotel_name'] = $hotelName;
				$hotel_array['hotel_URL'] = $hotelURL;
				$hotel_array['address_id'] = $address->getAddressId();
				$hotel_array['street_address'] = $address->getStreet();
				$hotel_array['city'] = $address->getCity();
				$hotel_array['state'] = $address->getStateOrProvince();
				$hotel_array['zip'] = $address->getPostalCode();
				$hotel_array['country_code'] = $country->getCountryCode();
				$hotel_array['country_name'] = $country->getCountryName();
				$hotel_array['country_currency'] = $country->getCountryCurrency();
				$hotel_array['star_rating_id'] = $starRating->getStarRatingId();
				$hotel_array['star_rating_code'] = $starRating->getStarRatingCode();
				$hotel_array['star_rating_description'] = $starRating->getStarRatingDescription();
				array_push($return_arr, $hotel_array);
				$total = $total + 1;
		}
	}
	$numHotels = array();
	if($total > 0) {
		$numHotels['count'] = $total;
		array_push($return_arr, $numHotels);
	}
	else {
		$numHotels['count'] = 0;
		array_push($return_arr, $numHotels);
	}
	
	print json_encode($return_arr);
}
else {
	$error = true;
	print json_encode($message);
}
?>
